import type { ResponsePaginationData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import { request } from 'umi';
import { message } from 'antd';
import { AddAdminBean, AdminItem, UpdateAdminBean, UpdateAdminIdentifyBean } from './data';


/**
 * 获取管理员列表
 * @param params
 */
export async function queryAdminList(params: {
  page?: number,
  pageSize?: number,
}): Promise<RequestData<AdminItem>> {
  try {
    const res: ResponsePaginationData<AdminItem> = await request('/admin/get_list', {
      params: {
        page: 1,
        pageSize: 10,
        ...params,
      },
      method: 'GET',
    });

    if (res?.code !== SUCCESS_CODE) {
      return { success: false, total: 0, data: [] };
    }

    return {
      success: res.code === SUCCESS_CODE,
      total: res.data.total,
      data: res.data.list,
    };
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

/**
 * 删除管理员
 * @param id 
 * @returns 
 */
export async function deleteAdmin(id: string): Promise<RequestData<any>> {
  const res = await request('/admin/delete', {
    method: 'POST',
    data: {
      id: id
    },
  });
  if (res.code !== SUCCESS_CODE) {
    message.warning(res.message)
  }
  return res;
}

/**
 * 修改管理员信息
 * @param data 
 * @returns 
 */
export async function updateAdminInfo(data: UpdateAdminBean): Promise<RequestData<any>> {
  const res = await request('/admin/update_info', {
    method: 'POST',
    data: data
  });
  if (res.code !== SUCCESS_CODE) {
    message.warning(res.message)
  }
  return res;
}

/**
 * 修改管理员密码
 * @param data 
 * @returns 
 */
export async function updateAdminPwd(data: {
  id: string,
  pwd: string
}): Promise<RequestData<any>> {
  const res = await request('/admin/update_pwd', {
    method: 'POST',
    data: data
  });
  if (res.code !== SUCCESS_CODE) {
    message.warning(res.message)
  }
  return res;
}

// 注册管理员
export async function registerAdmin(data: AddAdminBean) {
  return await request(`/admin/register`, {
    data: data, method: 'POST'
  });
}

//根据部门查询账户
export async function queryPositionByDep(department_id: string) {
  return await request(`/structure/view_by_dep`, {
    params: {
      department_id,
    },
  });
}

//根据职位获取上级角色管理员
export async function querySuperiorMent(position_id: string) {
  return await request(`/admin/get_belong`, {
    params: {
      position_id,
    },
  });
}

//获取该职位管理员列表
export async function queryAdminListByPosition(position_id: string) {
  return await request(`/admin/get_admin_by_position`, {
    params: {
      position_id,
    },
  });
}

//修改管理员身份
export async function editAdminIdentify(data: UpdateAdminIdentifyBean): Promise<RequestData<any>> {
  const res = await request('/admin/change_identify', {
    method: 'POST',
    data: data
  });
  if (res.code !== SUCCESS_CODE) {
    message.warning(res.message)
  }
  return res;
}